<?php
/* @var $this RebatecommissionController */
/* @var $model Rebatecommission */
$this->breadcrumbs=array(
	Yii::t('base','Rebatecommission')=>array('show'),
	Yii::t('base','Manage'),
);

Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
	$('.search-form').toggle();
	return false;
});
$('.search-form form').submit(function(){
	$('#rebatecommission-grid').yiiGridView('update', {
		data: $(this).serialize()
	});
	return false;
});
");
?>

<h1><?php echo Yii::t('base','Manage Rebatecommissions')?></h1>

<?php 
echo Yii::t('base','<p>
You may optionally enter a comparison operator (<b>&lt;</b>, <b>&lt;=</b>, <b>&gt;</b>, <b>&gt;=</b>, <b>&lt;&gt;</b>
or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.
</p>');
?>

<?php echo CHtml::link(Yii::t('base','Advanced Search'),'#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search2',array(
	'model'=>$model,
)); 
$model->type=1;
?>
</div><!-- search-form -->
<?php 
if($_POST['Rebatecommission']==""){
	$sql="SELECT SUM(package_price),company_number,DATE_FORMAT(sale_time,'%Y-%m'),COUNT(*),SUM(bonus) FROM zy_bonus where company_number!=0 GROUP BY company_number";
}else{
	$agentcomany=Agentcompany::model()->find('agent_id='.$_POST['Rebatecommission']['agent_company_id']);
	$company_number=$agentcomany->agent_number;
	$sql="SELECT SUM(package_price),company_number,DATE_FORMAT(sale_time,'%Y-%m'),COUNT(*),SUM(bonus) FROM zy_bonus where company_number=".$company_number." AND DATE_FORMAT(sale_time,'%Y-%m')= '".$_POST['Rebatecommission']['date']."' GROUP BY company_number";
}
$connection = Yii::app()->db;
$command = $connection->createCommand($sql);
$result = $command->queryAll();
?>
<?php for($i=0;$i<count($result);$i++){
	$agent=AgentCompany::model()->find('agent_number='.$result[$i]['company_number']);
	$agent_company_id=$agent->agent_id;
	$rebatecommission=Rebatecommission::model()->find("agent_company_id=".$agent_company_id." AND date='".$result[$i]["DATE_FORMAT(sale_time,'%Y-%m')"]."'");
}
	if($rebatecommission==""){
	?>
<div id="rebatecommission-grid" class="grid-view">
<table class="responsive table table-striped table-bordered">
<thead>
<tr>
<th id="rebatecommission-grid_c0">代理人姓名</th><th id="rebatecommission-grid_c1">年月</th><th id="rebatecommission-grid_c2">返点比例(%)</th><th id="rebatecommission-grid_c3">返点规则</th><th id="rebatecommission-grid_c4">数量</th><th id="rebatecommission-grid_c5">销售金额(澳币)</th><th id="rebatecommission-grid_c6">提成金额(澳币)</th><th id="rebatecommission-grid_c7">总计(澳币)</th><th class="button-column" id="rebatecommission-grid_c8">操作</th></tr>
</thead>
<tbody>
<tr class="odd">
<?php for($i=0;$i<count($result);$i++){
	$agent=AgentCompany::model()->find('agent_number='.$result[$i]['company_number']);
	$number=$result[$i]['COUNT(*)'];
	$fandian=Fandian::model()->find("agent_type=".$agent->agent_type."  AND $number between min and max");
	?>
<td><?php 
$user=User::model()->find('user_id='.$agent->user_id);
echo $user->user_name;
?></td><td><?php echo $result[$i]["DATE_FORMAT(sale_time,'%Y-%m')"];?></td>
<td><?php echo $fandian->rate;?></td><td><a href="<?php echo Yii::app()->request->baseUrl; ?>/fandian/<?php echo $fandian->id;?>"><?php echo $fandian->id;?></a></td>
<td><?php echo $result[$i]['COUNT(*)'];?></td><td><?php echo $result[$i]['SUM(package_price)'];?></td>
<td><?php echo $result[$i]['SUM(bonus)'];?></td><td><?php echo $result[$i]['SUM(package_price)']*$fandian->rate/100+$result[$i]['SUM(bonus)'];?></td>
<td class="button-column"><a class="btn btn-small" title="查看" href="<?php echo Yii::app()->request->baseUrl; ?>/bonus/admin?company_number=<?php echo $result[$i]['company_number'];?>">查看详细</a><a class="btn btn-small" title="审核" href="<?php echo Yii::app()->request->baseUrl; ?>/rebatecommission/show2?company_number=<?php echo $result[$i]['company_number'];?>&date=<?php echo $result[$i]["DATE_FORMAT(sale_time,'%Y-%m')"] ?>">审核</a></td></tr>
<?php } ?>
</tbody>
</table>
</div> 
<?php } ?>